package com.lm.selenium.config;

import com.lm.selenium.service.ProxyService;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SeleniumConfig {
    @Bean(destroyMethod = "quit")
    public WebDriver webDriver(ProxyService proxyService) {
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1920,1080");

        // 设置代理
        if(proxyService.hasAvailableProxy()) {
            Proxy proxy = new Proxy();
            proxy.setHttpProxy(proxyService.getRandomProxy());
            options.setProxy(proxy);
        }

        return new ChromeDriver(options);
    }
}
